package com.gla.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {
    final static String key = "207yyds";//盐
    final static Integer ttl = 1*60*60*1000;

    static public String generateToken( Integer id ) {
        String s = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .signWith(SignatureAlgorithm.HS512, key)
                .setId(id.toString())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis()+ttl))
                .compact();
        return s;
    }

    static public Integer parseToken( String token ) {
        Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
        return Integer.parseInt(claims.getId());
    }
}